﻿using System;
using System.Windows.Forms;
using MentalAlchemy.Molecules.MachineLearning;

namespace MentalAlchemy.Compounds.MachineLearning
{
	/// <summary>
	/// Class to represent properties of the neural network layer.
	/// </summary>
	public partial class NeuralLayerProperties : UserControl
	{
		private bool hiddenLayer;

		#region - Public properties. -
		/// <summary>
		/// Selected activation function.
		/// </summary>
		public ActivationFunction ActivationFunction
		{
			get
			{
				var name = (string)ActivationCombo.SelectedItem;
				return ActivationFunctions.GetActivationFunction(name);
			}
			set
			{
				var idx = ActivationCombo.FindString(value.Method.Name);
				ActivationCombo.SelectedIndex = idx;
			}
		}

		/// <summary>
		/// Number of nodes in the layer.
		/// </summary>
		public int LayerSize 
		{ 
			get { return (int) SizeNumeric.Value; }
			set
			{
				var temp = Math.Min (value, SizeNumeric.Maximum);
				SizeNumeric.Value = Math.Max(temp, SizeNumeric.Minimum);
			}
		}

		/// <summary>
		/// Check box to (un)select the layer.
		/// </summary>
		public CheckBox LayerCheckBox { get { return LayerCheck;} }

		/// <summary>
		/// Indicates whether the layer is hidden or not.
		/// </summary>
		public bool HiddenLayer 
		{ 
			get
			{
				return hiddenLayer;
			}
			set
			{
				hiddenLayer = value;

				if (!hiddenLayer)
				{
					LayerCheckBox.Checked = true;
					LayerCheckBox.Enabled = false;
					SizeNumeric.Enabled = false;
					ActivationCombo.Enabled = true;
				}
				else
				{
					LayerCheckBox.Enabled = true;

					ActivationCombo.Enabled = LayerCheck.Checked;
					SizeNumeric.Enabled = LayerCheck.Checked;
				}
			} 
		}

		/// <summary>
		/// Text by the check-box.
		/// </summary>
		public string Text
		{
			get { return LayerCheck.Text; }
			set { LayerCheck.Text = value; }
		}
		#endregion

		#region - Construction. -
		public NeuralLayerProperties()
		{
			InitializeComponent();

			var funcs = ActivationFunctions.Functions();
			ActivationCombo.Items.AddRange(funcs);
			ActivationCombo.SelectedIndex = 0;
		} 
		#endregion

		#region - Event handlers. -
		private void LayerCheck_CheckedChanged(object sender, EventArgs e)
		{
			if (HiddenLayer)
			{
				ActivationCombo.Enabled = LayerCheck.Checked;
				SizeNumeric.Enabled = LayerCheck.Checked;
			}
		} 
		#endregion
	}
}
